using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace KFGame.Modes
{
    public class KFSeralPortEventConfigMode : KFMode
    {
        
        public static KFSeralPortEventConfigMode getModes()
        {
               if (!editObject)  
               editObject = new GameObject("KF_Config_edit");
            var mode =  editObject.AddComponent<KFSeralPortEventConfigMode>();
            return mode;
        }

        public string filename = "kfseralportevents.ini";


        /// <summary>
        /// 所有事件
        /// </summary>
        /// <typeparam name="KFKeyEventMode"></typeparam>
        /// <returns></returns>
        [SerializeField]
        [Tooltip("IO接口记录集")]
        public List<KFSeralPortEventMode> eventModes = new List<KFSeralPortEventMode>();

        public override string FileName()
        {
            return filename;
        }

        public override void save()
        {
            string iniPath = Application.streamingAssetsPath + "/KFConfig/" + filename;
            INIParser iNIParser = new INIParser();
            iNIParser.Open(iniPath);
            iNIParser.clean();

            for (var i = 0; i < eventModes.Count; i++)
            {
                if (eventModes[i].modeName == null)
                    eventModes[i].modeName = eventModes[i].name + i;

                iNIParser.WriteValue(eventModes[i].modeName, "enter", eventModes[i].enter);
                iNIParser.WriteValue(eventModes[i].modeName, "name", eventModes[i].name);

                iNIParser.WriteValue(eventModes[i].modeName, "com", eventModes[i].com);
                iNIParser.WriteValue(eventModes[i].modeName, "tag", eventModes[i].tag);
                iNIParser.WriteValue(eventModes[i].modeName, "type", eventModes[i].type);
                iNIParser.WriteValue(eventModes[i].modeName, "dataType", eventModes[i].dataType);
                iNIParser.WriteValue(eventModes[i].modeName, "dataIndex", eventModes[i].dataIndex);
                iNIParser.WriteValue(eventModes[i].modeName, "dataLength", eventModes[i].dataLength);
                iNIParser.WriteValue(eventModes[i].modeName, "checkdata", eventModes[i].checkdata);
                iNIParser.WriteValue(eventModes[i].modeName, "templateData", eventModes[i].templateData);
                iNIParser.WriteValue(eventModes[i].modeName, "sendnumber", eventModes[i].sendnumber);
                iNIParser.WriteValue(eventModes[i].modeName, "sendtime", eventModes[i].sendtime);
                iNIParser.WriteValue(eventModes[i].modeName, "fiterName", eventModes[i].fiterName);
                iNIParser.WriteValue(eventModes[i].modeName, "isEnd", eventModes[i].isEnd);
                iNIParser.WriteValue(eventModes[i].modeName, "EndDataName", eventModes[i].EndDataName);
                
                iNIParser.WriteValue(eventModes[i].modeName, "des", eventModes[i].des);

            }
            iNIParser.save();

        }

        public override void LoadDatas()
        {
            string iniPath = Application.streamingAssetsPath + "/KFConfig/" + filename;
            INIParser iNIParser = new INIParser();
            iNIParser.Open(iniPath);
            foreach (var item in iNIParser.SectionNames())
            {
                KFSeralPortEventMode mode = new KFSeralPortEventMode();
                mode.modeName = item;

                mode.enter = iNIParser.ReadValue(item, "enter", false);
                mode.name = iNIParser.ReadValue(item, "name", "");
                mode.com = iNIParser.ReadValue(item, "com", "");
                mode.tag = iNIParser.ReadValue(item, "tag", "");
                mode.type = iNIParser.ReadValue(item, "type", "read");
                mode.dataType = iNIParser.ReadValue(item, "dataType", "08");
                mode.dataIndex = iNIParser.ReadValue(item, "dataIndex", 0);
                mode.dataLength = iNIParser.ReadValue(item, "dataLength", 1);
                mode.checkdata = iNIParser.ReadValue(item, "checkdata", "07");
                mode.templateData = iNIParser.ReadValue(item, "templateData", "");
                mode.sendnumber = iNIParser.ReadValue(item, "sendnumber", 1);
                mode.sendtime = iNIParser.ReadValue(item, "sendtime", 0.1);
                mode.fiterName = iNIParser.ReadValue(item, "fiterName", "");
                mode.isEnd = iNIParser.ReadValue(item, "isEnd", false);
                mode.EndDataName = iNIParser.ReadValue(item, "EndDataName", "");
                mode.des = iNIParser.ReadValue(item, "des", "");
                eventModes.Add(mode);



            }
        }
    }
}